#!/bin/bash

# tptable
#
# Generate a table of transform parameters
#
# Usage:
# 
# tptable -d <dirname> -E <?> -R <?> [-F]
#
#   -d: the directory name containing the tp-files
#   -E: the elastix level for which a tptable is desired
#   -R: the resolution for which a tptable is desired
#   -F: force (default false): if set, old results are overwritten
#
# The result is a table:  <dirname>/tptable.<E>.R<R>.txt
# In this table the parameters are plotted. Each row is one iteration.
#
#

function PrintHelp()
{
  echo "Usage:"
	echo "pxtptable"
	echo "  -d      directory"
	echo "  [-E]    elastix level, default 0"
	echo "  [-R]    resolution level, default all"
	echo "  [-F]    force to overwrite old results, default false"
}

# Check for PrintHelp
if [[ $1 == "--help" ]]
then
	PrintHelp
	exit 64
fi

# Check for the number of arguments
if [ $# -lt 2 ] || [ $# -gt 8 ]
then
  echo "ERROR: not enough arguments!"
  echo "Call \"pxtptable --help\" for help."
  exit 64
fi

# Default values.
elastixlevel=0
resolution=99
force="false"

# Get the command line arguments.
while getopts "d:E:R:F" argje
do
  case $argje in 
    d) tpdir="$OPTARG";;
    E) elastixlevel="$OPTARG";;
    R) resolution="$OPTARG";;
    F) force="true";;
    *) echo "ERROR: Wrong arguments!"; exit 65;;
  esac
done

# Some shortcuts.
E="$elastixlevel"
R="$resolution"

# Check the command line arguments.
if [ ! -e "$tpdir" ]
then
  echo "ERROR: $tpdir does not exist. Quitting tptable."
  exit 66
fi

# Check which resolution levels are required.
if [ "$R" -eq "99" ]
then
	i=0; Rarray="";
	while [ 1 ]
	do
		file=$tpdir"/TransformParameters."$E".R"$i".It0000000.txt"
		if [ -e $file ]
		then
			Rarray=$Rarray" "$i
			let "i+=1"
		else
			break
		fi
	done
else
	Rarray=$R
fi

if [[ "$Rarray" == "" ]]
then
	echo "ERROR: no (finished) resolution information."
	exit 65;
fi

# Loop over all resolutions.
for R in $Rarray
do
	# Get the current output file.
	outfile="$tpdir/tptable.$E.R$R.txt"

	# Check for the existence of this file.
	if [ ! -e "$outfile" ]
	then
  	# create, to avoid that another thread starts to work on this one.
	  > $outfile
	else
  	# If the file exists, quit, unless -F was used.
	  if [ "$force" = "true" ]
  	then
    	# Overwrite old results.
	    > $outfile
	  else
  	  # Quit.
	    echo "ERROR: $outfile already exists. Quitting tptable."
  	  exit 67
	  fi
	fi

	# Collect all information
	itnr="0"
	tpfile=$tpdir"/TransformParameters."$E".R"$R".It0000000.txt"

	# For all iterations, look into the Transformparameters file
	while [ -e "$tpfile" ]
	do
 	  # first try to find "(TransformParameters "
	  thisline=`less $tpfile | grep "(TransformParameters "`

  	if [ "$thisline" = "" ]
	  then
	    # find // (TransformParameters) and the line after it
  	  # remove the // (TransformParameters) 
    	# remove the //
  	    thisline=`less $tpfile | grep "// (TransformParameters)" -A 1 | grep -v "// (TransformParameters)" | sed "s/\///g"`
	  else
	    # remove the "(TransformParameters" part and the ")"
  	  thisline=`echo $thisline | cut -f3 -ds | sed "s/)//g"`
	  fi

		# Check for file existence
	  if [ "$thisline" = "" ] 
  	then
    	echo "ERROR: no transform parameters found at iteration $itnr"  
	    exit 68;
  	fi
    
	  # print to output table
  	echo $thisline >> $outfile

	  # determine next tpfile name
  	let "itnr=itnr+1"
	  itstring=`printf '%07d' $itnr`
  	tpfile="$tpdir/TransformParameters.$E.R$R.It$itstring.txt"

	done
done

# Exit function, return success
exit 0

